<template>
  <div class="xtx-pagination">
    <a @click="changePager(myCurrentPage - 1)" v-if="myCurrentPage > 1" href="javascript:;">上一页</a>
    <a v-else href="javascript:;" class="disabled">上一页</a>
    <span v-if="pager.start > 1">...</span>
    <a v-for="item in pager.btnArr " :key="item" href="javascript:;" @click="changePager(myCurrentPage = item)"
      :class="{ active: item == myCurrentPage }">{{ item }}</a>
    <span v-if="pager.end < pager.pageCount">...</span>
    <a @click="changePager(myCurrentPage + 1)" v-if="myCurrentPage < pager.pageCount" href="javascript:;">下一页</a>
    <a v-else href="javascript:;" class="disabled">下一页</a>
  </div>
</template>
<script>
import { computed, ref, watch } from 'vue'
export default {
  name: 'XtxPagination',
  props: {
    total: {
      //数据总条数
      type: Number,
      default: 100
    },
    pageSize: {
      //每页数据多少条
      type: Number,
      default: 10
    },
    currentPage: {
      //当前显示的页码
      type: Number,
      default: 1
    }
  },
  setup(props,{emit}) {
    //定义驱动功能所需要的数据
    //1.显示页码按钮个数
    const count = 5
    //2.当前显示的页码
    const myCurrentPage = ref(1)
    //3.数据总条数
    const myTotal = ref(100)
    //3.每页数据多少条
    const myPageSize = ref(10)
    //其他数据
    const pager = computed(() => {
      //总页数
      const pageCount = Math.ceil(myTotal.value / myPageSize.value)
      //根据按钮个数以及当前页码  计算出 ... 3 4 5 6 7 ...  类似结构
      //1.理想情况下
      const offset = Math.floor(count / 2)
      let start = myCurrentPage.value - offset
      let end = start + count - 1
      //2.当 start<1 时
      if (start < 1) {
        start = 1
        end = (start + count - 1) > pageCount ? pageCount : (start + count - 1)
      }
      //3.当 end > pageCount 时
      if (end > pageCount) {

        end = pageCount
        start = (end - count + 1) < 1 ? 1 : (end - count + 1)
      }
      //按钮数组
      const btnArr = []
      for (let i = start; i < end; i++) {
        btnArr.push(i)
      }
      return {
        pageCount,
        start,
        end,
        btnArr
      }
    })
    //监听props数据的变化，更新当前组件的数据
    watch(props, () => {
      myTotal.value = props.total
      myPageSize.value = props.pageSize
      myCurrentPage.value = props.currentPage
    }, { immediate: true })

    //切换分页的功能
    const changePager = (value) => {
      myCurrentPage.value = value
      //通知父组件修改页码重新发起请求
      emit('current-page',value)
    }
    return {
      myCurrentPage,
      pager,
      changePager
    }
  }
}
</script>
<style scoped lang="less">
.xtx-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;

  >a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;

    &:hover {
      color: @xtxColor;
    }

    &.active {
      background: @xtxColor;
      color: #fff;
      border-color: @xtxColor;
    }

    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;

      &:hover {
        color: #333
      }
    }
  }

  >span {
    margin-right: 10px;
  }
}
</style>